package com.example.service.impl;

import com.example.common.ErrorCode;
import com.example.exception.BusinessException;
import com.example.mapper.HotelMapper;
import com.example.model.hotel.Hotel;
import com.example.model.hotel.HotelAddRequest;
import com.example.model.hotel.HotelRoomCount;
import com.example.model.hotel.HotelUpdateRequest;
import com.example.service.HotelService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class HotelServiceImpl implements HotelService {

    @Resource
    private HotelMapper hotelMapper;

    @Override
    public Long addHotel(HotelAddRequest hotelAddRequest) {
        //检查酒店是否存在
        String name=hotelAddRequest.getHotelName();
        Hotel tempHotel=hotelMapper.getByName(name);
        if(tempHotel!=null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"酒店已存在");
        }
        Hotel hotel=new Hotel();
        hotel.setHotelName(hotelAddRequest.getHotelName());
        hotel.setCity(hotelAddRequest.getCity());
        hotel.setAddress(hotelAddRequest.getAddress());
        hotel.setPhone(hotelAddRequest.getPhone());
        hotelMapper.save(hotel);
        return hotel.getId();
    }

    @Override
    public Boolean update(HotelUpdateRequest hotelUpdateRequest) {
        return hotelMapper.update(hotelUpdateRequest);
    }

    @Override
    public List<Hotel> getHotels(Hotel hotel) {
        return hotelMapper.getHotels(hotel);
    }

    @Override
    public List<Hotel> getAllHotels() {
        return hotelMapper.searchAll();
    }

    @Override
    public List<HotelRoomCount> getHotelCount() {
        List<HotelRoomCount> hotelRoomCount = hotelMapper.getHotelRoomCount();
        if(hotelRoomCount==null){
            throw new BusinessException(ErrorCode.SQL_ERROR);
        }
        log.info(hotelRoomCount.toString());
        return hotelRoomCount;
    }
}
